﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluidKit.Controls;
using System.Windows.Controls;
using System.Windows;
using Ares.Controller;
using Ares.GUI.Views;
using Ares.Engine;
using System.IO;
using Ares.GUI.Models;

namespace Ares.GUI.Controller
{
    public class ProjectShell : BaseController
    {
        public TransitionPresenter Presenter;
        public SlideTransition SlideTransition;
        public UserControl Shell;

        public string ActiveView;

        private UserControl fTransitioningView1;
        private UserControl fTransitioningView2;

        public UserControl TransitioningView1
        {
            get
            {
                return fTransitioningView1;
            }

            set
            {
                fTransitioningView1 = value;
                OnPropertyChanged("TransitioningView1");
            }
        }

        public UserControl TransitioningView2
        {
            get
            {
                return fTransitioningView2;
            }

            set
            {
                fTransitioningView2 = value;
                OnPropertyChanged("TransitioningView2");
            }
        }

        public Dictionary<string, UserControl> Views;

        public ProjectShell()
        {
            Views = new Dictionary<string, UserControl>();

            //Initialize all available views
            Views.Add("ProjectHome", new ProjectView());
            Views.Add("ProjectView", new NewProjectView());
            Views.Add("ProjectRevert", new RevertProjectView());

            TransitioningView1 = Views["ProjectHome"];
            TransitioningView2 = Views["ProjectView"];

            Mediator.Register(this, new[] {
                Messages.DoNewProject,
                Messages.SaveProject,
                Messages.InitTransition,
                Messages.CancelNewProject,
                Messages.RevertProject,
                Messages.EditProject
            });

            ActiveView = "TransitioningView1";

        }

        public void Init()
        {
            SlideTransition.Direction = Direction.RightToLeft;
            SlideTransition.Duration = new Duration(new System.TimeSpan(0, 0, 0, 0, 1));
            Presenter.Transition = SlideTransition;
            ActiveView = "TransitioningView1";
            Presenter.ApplyTransition("_TransitioningView2", "_TransitioningView1");
        }

        public void DoTransition(String Path, Direction TransitionDirection)
        {
            if (!String.IsNullOrWhiteSpace(Path))
            {
                try
                {
                    SlideTransition.Direction = TransitionDirection;
                    SlideTransition.Duration = new Duration(new System.TimeSpan(0, 0, 0, 0, 500));
                    Presenter.Transition = SlideTransition;

                    if (ActiveView == "TransitioningView1")
                    {
                        if (TransitioningView1 != Views[Path])
                        {
                            TransitioningView2 = null;
                            TransitioningView2 = Views[Path];
                            ActiveView = "TransitioningView2";
                            Presenter.ApplyTransition("_TransitioningView1", "_TransitioningView2");
                        }
                    }
                    else
                    {
                        if (TransitioningView2 != Views[Path])
                        {
                            TransitioningView1 = Views[Path];
                            ActiveView = "TransitioningView1";
                            Presenter.ApplyTransition("_TransitioningView2", "_TransitioningView1");
                        }
                    }
                }
                catch
                {

                }
            }
        }

        public override void MessageNotification(string message, object args)
        {
            switch (message)
            {
                case Messages.DoNewProject:
                    DoTransition("ProjectView", Direction.RightToLeft);
                    break;

                case Messages.EditProject:
                    DoTransition("ProjectView", Direction.RightToLeft);
                    break;

                case Messages.SaveProject:
                    if (args != null)
                    {
                        BackupProject project = (BackupProject)args;
                        Directory.CreateDirectory(Settings.ProjectsPath);
                        BackupProject.SaveBackupProject(Path.Combine(Settings.ProjectsPath, project.ProjectGuid + Settings.ProjectFileExtension), project);
                    }
                    
                    DoTransition("ProjectHome", Direction.LeftToRight);
                    break;

                case Messages.InitTransition:
                    Init();
                    break;

                case Messages.CancelNewProject:
                    DoTransition("ProjectHome", Direction.LeftToRight);
                    break;

                case Messages.RevertProject:
                    DoTransition("ProjectRevert", Direction.RightToLeft);
                    break;
            }
        }
    }
}
